﻿package blob.characters
{
    import blob.levelstates.*;
    import org.flixel.*;

    public class Character extends FlxSprite
    {
        protected var _moveSpeed:int;
        protected var _jumpPower:int;
        protected var _isWalkingLeft:Boolean;
        protected var _isWalkingRight:Boolean;
        protected var _externalForce:Number;
        private var SoundLand:Class;

        public function Character(param1:Number, param2:Number)
        {
            this.SoundLand = Character_SoundLand;
            super(param1, param2);
            this._moveSpeed = 0;
            this._jumpPower = 0;
            maxVelocity.x = 350;
            maxVelocity.y = 500;
            acceleration.y = 1000;
            drag.x = 400;
            (FlxG.state as PlayState).characterGroup.add(this);
            this._isWalkingLeft = false;
            this._isWalkingRight = false;
            this._externalForce = 0;
            return;
        }// end function

        public function split() : Array
        {
            return new Array();
        }// end function

        public function merge(param1:Character) : Character
        {
            return this;
        }// end function

        public function get moveSpeed() : int
        {
            return this._moveSpeed;
        }// end function

        public function get jumpPower() : int
        {
            return this._jumpPower;
        }// end function

        public function size() : int
        {
            return 1;
        }// end function

        public function setCouldntMerge() : void
        {
            return;
        }// end function

        public function applyForce(param1:int) : void
        {
            this._externalForce = this._externalForce + param1;
            return;
        }// end function

        public function startWalking(param1:Boolean) : void
        {
            if (param1)
            {
                this._isWalkingRight = false;
                this._isWalkingLeft = true;
            }
            else
            {
                this._isWalkingLeft = false;
                this._isWalkingRight = true;
            }
            return;
        }// end function

        public function stopWalking() : void
        {
            this._isWalkingLeft = false;
            this._isWalkingRight = false;
            return;
        }// end function

        protected function isOverlapping(param1:Character) : Boolean
        {
            var _loc_3:FlxSprite = null;
            var _loc_2:* = PlayState(FlxG.state);
            for each (_loc_3 in _loc_2.doors.members)
            {
                
                if (_loc_3.alive && param1.overlaps(_loc_3))
                {
                    return true;
                }
            }
            if (param1.overlaps(_loc_2._map1) || _loc_2.collidableMechanisms.members.length != 0 && param1.overlaps(_loc_2.collidableMechanisms) || _loc_2.blocks.members.length != 0 && param1.overlaps(_loc_2.blocks) || _loc_2.movingBlocks.members.length != 0 && param1.overlaps(_loc_2.movingBlocks))
            {
                return true;
            }
            return false;
        }// end function

        override public function update() : void
        {
            var _loc_1:Number = 0;
            if (justTouched(FLOOR))
            {
                FlxG.play(this.SoundLand, 0.4);
            }
            if (this._isWalkingLeft)
            {
                _loc_1 = -this._moveSpeed;
            }
            else if (this._isWalkingRight)
            {
                _loc_1 = this._moveSpeed;
            }
            if (this.isTouching(FlxObject.LEFT) || this.isTouching(FlxObject.RIGHT))
            {
                this._externalForce = 0;
            }
            else if (this.isTouching(FlxObject.DOWN))
            {
                if (this._externalForce > 0)
                {
                    this._externalForce = Math.max(this._externalForce - drag.x / 10, 0);
                }
                else
                {
                    this._externalForce = Math.min(this._externalForce + drag.x / 10, 0);
                }
            }
            else if (this._externalForce > 0)
            {
                this._externalForce = Math.max(this._externalForce - drag.x / 100, 0);
            }
            else
            {
                this._externalForce = Math.min(this._externalForce + drag.x / 100, 0);
            }
            _loc_1 = _loc_1 + this._externalForce;
            velocity.x = _loc_1;
            return;
        }// end function

    }
}
